import requests, json, hashlib, base64, time

# 公用参数
APP_ID = "406595";
APP_SECRET = "lpeFgRmpkuH7JhbSqsGMWqkb";
V = "2.0";
HOST = "http://test.api.fabigbig.com:8888/api/";
timestamp = time.strftime('%Y%m%d%H%M%S')
# timestamp = '20211223101252'
print(timestamp)

# 注册账号
url = HOST + "account_register.api"
open_id = 'jcpython003'
account_type = '1'
# sha1
sh = hashlib.sha1()
sh.update((APP_SECRET + account_type + open_id).encode('utf-8'))
print(APP_SECRET + account_type + open_id)
# md5
md = hashlib.md5()
md.update(timestamp.encode('utf-8'))
# 内层md5和sha1加密
sha1 = (APP_ID.encode('utf-8')) + str(md.hexdigest().upper()).encode('utf-8') + sh.hexdigest().upper().encode('utf-8')
# 重新创建一个sha1加密方法，覆盖之前的sha1
sh = hashlib.sha1()
# 外层sha1加密
sh.update(sha1)
# base64加密sha1参数，形成最终加密摘要
Msg_digest = base64.b64encode(sh.hexdigest().upper().encode('utf-8')).decode()
# 打印摘要
print(Msg_digest)
# data，存放参数
data = {
    'app_id': APP_ID,
    'open_id': open_id,
    'timestamp': timestamp,
    'account_type': account_type,
    'v': '2.0',
    'msg_digest': Msg_digest
}
# 发送请求参数，返回状态码，成功则200
response = requests.post(url, data=data);
print(response)
# 判断状态码，200的时候读取参数
if (response.status_code == 200):
    # 获取相应内容
    content = response.text

# 打印接口返回参数
print(content)

# 个人实名认证
url = HOST + "get_person_verify_url.api"
customer_id = 'FDA2A7285623E54E4D07FA5F640F14B6'
verified_way = '0'
page_modify = '1'
cert_flag = '1'
# sha1
return_url = "https://www.baidu.com"
sh = hashlib.sha1()
sh.update((APP_SECRET + cert_flag + customer_id + page_modify + return_url + verified_way).encode('utf-8'))
# md5
md = hashlib.md5()
md.update(timestamp.encode('utf-8'))
# 内层md5和sha1加密
sha1 = (APP_ID.encode('utf-8')) + str(md.hexdigest().upper()).encode('utf-8') + sh.hexdigest().upper().encode('utf-8')
# 重新创建一个sha1加密方法，覆盖之前的sha1
sh = hashlib.sha1()
# 外层sha1加密
sh.update(sha1)
# base64加密sha1参数，形成最终加密摘要
Msg_digest = base64.b64encode(sh.hexdigest().upper().encode('utf-8')).decode()
# 打印摘要
print(Msg_digest)
# data，存放参数
data = {
    'app_id': APP_ID,
    'customer_id': customer_id,
    'timestamp': timestamp,
    'verified_way': verified_way,
    'page_modify': page_modify,
    'v': '2.0',
    'msg_digest': Msg_digest,
    'cert_flag': cert_flag,
    "return_url": return_url
}
# 发送请求参数，返回状态码，成功则200
response = requests.post(url, data=data);
print(response)
# 判断状态码，200的时候读取参数
if (response.status_code == 200):
    # 获取相应内容
    content = response.text

# 打印接口返回参数
print(content)
# 转换json格式
analysis = json.loai吧ds(content);
# 取url值
url = analysis['data']['url']
# base解析地址
str_url = base64.b64decode(url).decode("utf-8")
# 打印地址
print(str_url)
